package com.pioneer.controller.archive;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pioneer.common.annotation.Log;
import com.pioneer.common.core.controller.BaseController;
import com.pioneer.common.core.domain.AjaxResult;
import com.pioneer.common.enums.BusinessType;
import com.pioneer.web.archive.domain.ArcInventory;
import com.pioneer.web.archive.service.IArcInventoryService;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 存货管理
 *
 * @author hlm
 */
@RestController
@RequestMapping("/archive/inventory")
public class ArcInventoryController extends BaseController {

    @Resource
    private IArcInventoryService inventoryService;

    /**
     * 获取存货管理列表
     *
     * @param inventory 查询条件
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('archive:inventory:list')")
    @GetMapping("/list")
    public AjaxResult list(ArcInventory inventory) {
        boolean isPage = startPage();
        List<ArcInventory> list = inventoryService.list(Wrappers.query(inventory));
        return success(isPage, list);
    }

    /**
     * 根据存货管理id获取详细信息
     *
     * @param inventoryId 存货管理id
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('archive:inventory:query')")
    @GetMapping(value = "/{inventoryId}")
    public AjaxResult getInfo(@PathVariable Long inventoryId) {
        return AjaxResult.success(inventoryService.getById(inventoryId));
    }

    /**
     * 新增存货管理
     *
     * @param inventory 存货管理
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('archive:inventory:add')")
    @Log(title = "存货管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody ArcInventory inventory) {
        if (inventoryService.checkCodeUnique(inventory)) {
            return AjaxResult.error("存货编码【" + inventory.getInventoryCode() + "】已存在，请重新输入");
        }
        return toAjax(inventoryService.save(inventory));
    }

    /**
     * 修改存货管理
     *
     * @param inventory 存货管理
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('archive:inventory:edit')")
    @Log(title = "存货管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody ArcInventory inventory) {
        if (inventoryService.checkCodeUnique(inventory)) {
            return AjaxResult.error("存货编码【" + inventory.getInventoryCode() + "】已存在，请重新输入");
        }
        return toAjax(inventoryService.updateById(inventory));
    }

    /**
     * 删除存货管理
     *
     * @param inventoryIds 存货管理id数组
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('archive:inventory:remove')")
    @Log(title = "存货管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{inventoryIds}")
    public AjaxResult remove(@PathVariable Long[] inventoryIds) {
        return toAjax(inventoryService.removeByIds(Arrays.asList(inventoryIds)));
    }
}
